package com.yuelife.service.impl;

import com.yuelife.dao.CDiscussionDao;
import com.yuelife.entity.CComment;
import com.yuelife.dao.CCommentDao;
import com.yuelife.entity.CDiscussion;
import com.yuelife.service.CCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CComment)表服务实现类
 *
 * @author makejava
 * @since 2020-06-12 15:55:15
 */
@Service("cCommentService")
public class CCommentServiceImpl implements CCommentService {
    @Resource
    private CCommentDao cCommentDao;
    @Autowired
    CDiscussionDao cDiscussionDao;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CComment queryById(Integer id) {
        return this.cCommentDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<CComment> queryAllByLimit(int offset, int limit) {
        return this.cCommentDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param cComment 实例对象
     * @return 实例对象
     */
    @Override
    public CComment insert(CComment cComment) {
        this.cCommentDao.insert(cComment);
        return cComment;
    }

    /**
     * 修改数据
     *
     * @param cComment 实例对象
     * @return 实例对象
     */
    @Override
    public CComment update(CComment cComment) {
        this.cCommentDao.update(cComment);
        return this.queryById(cComment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.cCommentDao.deleteById(id) > 0;
    }

    @Override
    public CDiscussion selectCommentByDiscussion(Integer id) {
        CDiscussion res = cDiscussionDao.selectItemById(id);
        return res;
    }

    @Override
    public void updateLike(Integer i,Integer id) {
        cCommentDao.updataLike(i,id);
    }

    @Override
    public List<CComment> getComment(Integer id) {
        return cCommentDao.getComment(id);
    }

    @Override
    public void updatereply(Integer discusion) {
        cCommentDao.updatereply(discusion);
    }

    @Override
    public List<CComment> selectAllUserComment(String username) {
        CComment cComment = new CComment();
        cComment.setCreatedBy(username);
        List<CComment> list = cCommentDao.queryAll(cComment);
        for (int i = 0;i<list.size();i++){
            CComment item = list.get(i);
            if (item.getStatus()==1){
                CDiscussion discussion = cDiscussionDao.queryById(item.getDiscusion());
                item.setDiscussion(discussion);
            }else if (item.getStatus()==2){
                CComment parent = cCommentDao.queryById(item.getDiscusion());
                CDiscussion discussion = cDiscussionDao.queryById(parent.getDiscusion());
                item.setDiscussion(discussion);
            }
        }
        return list;
    }
}